

package yf.mall.service.impl.app;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.response.R;
import com.yf.service.impl.BaseServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yf.mall.component.app.AppCourseComponent;
import yf.mall.dao.AppCourseDao;
import yf.mall.entity.AppCourseEntity;
import yf.mall.entity.AppCourseEntityExample;
import yf.mall.entity.custom.AppCourseParam;
import yf.mall.mapper.AppCourseExtMapper;
import yf.mall.service.app.AppCourseService;
import yf.mall.vo.AppCourseVo;
import yf.mall.vo.down.AppCourseDown;
import yf.mall.vo.up.AppCourseUp;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 账户记录Service实现类
 *
 *
 * 
 * @date Tue Jul 18 14:30:26 CST 2023
 */
@Service
public class AppCourseServiceImpl extends BaseServiceImpl<AppCourseEntity> implements AppCourseService {

    @Resource
    private AppCourseDao dao;

    @Resource
    private AppCourseExtMapper mapper;


    @Autowired
    public void setDao(AppCourseDao dao) {
        this.dao = dao;
        super.setBaseDao(dao);
    }


    @Override
    public List<AppCourseVo> findVo(AppCourseVo vo) {
        return dao.findVo(vo);
    }

    @Override
    public AppCourseVo getVo(Long id) {
        return dao.getVo(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveVo(AppCourseVo vo) {
        return dao.save(vo.getEntity());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateVo(AppCourseVo vo) {
        return dao.update(vo.getEntity());
    }


    @Override
    public void downLoad(HttpServletResponse response, List<AppCourseVo> list) {
        try {
            List<AppCourseDown> datas = list.stream().map(item -> new AppCourseDown(item)).collect(Collectors.toList());
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("测试", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), AppCourseDown.class).sheet("模板").doWrite(datas);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public void upload(InputStream file) {
        EasyExcel.read(file, AppCourseUp.class, new UploadDataListener()).sheet().doRead();
    }


    class UploadDataListener extends AnalysisEventListener<AppCourseUp> {


        @Override
        @Transactional(rollbackFor = Exception.class)
        public void invoke(AppCourseUp upVo, AnalysisContext analysisContext) {
            saveVo(upVo.toVo());
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        }

    }


    /**内部方法**/


    /**
     * 外部方法实现
     **/
    @Override
    public PageInfo<AppCourseEntity> page(AppCourseEntityExample example, Page<AppCourseEntity> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<AppCourseEntity> entityList = mapper.selectByExample(example);
        return new PageInfo(entityList);
    }

    @Override
    public List<yf.mall.vo.app.course.AppCourseVo> findByHot(Integer value) {
        List<AppCourseEntity> entityList = dao.findByHot(value);
        List<yf.mall.vo.app.course.AppCourseVo> appCourseVoList = entityList.stream().map(dxCourse -> {
            yf.mall.vo.app.course.AppCourseVo appCourseVo = appCourseComponent.convertCourse(dxCourse);
            return appCourseVo;
        }).collect(Collectors.toList());
        return appCourseVoList;
    }

    @Resource
    @Lazy
    private AppCourseComponent appCourseComponent;

    @Override
    public List<AppCourseEntity> findAll() {
        return dao.findAll();
    }
}
